package proyectoFC_2_3_3.koyicabuto;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class CustomCursorAdapterEventos extends android.widget.CursorAdapter{
	
	private LayoutInflater mLayoutInflater;
	private Context mContext;
	private static final String GOOGLE_GEOCODER = "http://maps.googleapis.com/maps/api/geocode/json?latlng=";
	
	private TabEventoGrupoActivity tabEventoGrupoActivity = null;
	
	public CustomCursorAdapterEventos(Context context, Cursor c, TabEventoGrupoActivity tabEventoGrupoActivity) {
        super(context, c);
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
        this.tabEventoGrupoActivity = tabEventoGrupoActivity;
    }
	
	@Override
	public void bindView(final View view, final Context context, final Cursor cursor) {
		
		final ViewHolder holder = (ViewHolder)view.getTag();
		holder.setTextViewNombre((TextView)view.findViewById(R.id.Nombre));
		holder.setTextViewDireccion((TextView)view.findViewById(R.id.Direccion));
		holder.setTextViewComentario((TextView)view.findViewById(R.id.Comentario));
		holder.setBotonPapelera((Button)view.findViewById(R.id.Papelera));
		
		Button papelera = holder.getBotonPapelera();
		papelera.setBackgroundResource(R.drawable.ic_menu_delete_48);
		papelera.setVisibility(View.INVISIBLE);
		papelera.setTag(new Integer(cursor.getPosition()));
		
		
		papelera.setOnClickListener(new OnClickListener() {	
			public void onClick(View v) {
				Button papelera = holder.getBotonPapelera();
				Integer currentPosition = (Integer)papelera.getTag();
				if(cursor.moveToPosition(currentPosition)){
					tabEventoGrupoActivity.eliminaEvento(cursor.getLong(0), cursor.getString(1));
				}
			}
		});
		
		if(cursor.getString(5) != null && !cursor.getString(5).isEmpty()){
			
			String comentario = cursor.getString(5);
			if(comentario.length() > 70){
				comentario = comentario.substring(0, 67);
				comentario = comentario + "...";
			}
			holder.getTextViewComentario().setText(comentario);
		}
		
		holder.getTextViewDireccion().setText(getAddressFromGPSData(cursor.getDouble(3), cursor.getDouble(2)));
		
		holder.getTextViewNombre().setText(cursor.getString(1));
		
		
		view.setOnLongClickListener(new View.OnLongClickListener() {
			
			public boolean onLongClick(View v) {
				
				Animation animationRight = AnimationUtils.loadAnimation(v.getContext(), android.R.anim.slide_out_right);
				Animation animationLeft = AnimationUtils.loadAnimation(v.getContext(), android.R.anim.slide_in_left);
				Button papelera = holder.getBotonPapelera();
				if(papelera == null){
					papelera = (Button)view.findViewById(R.id.Papelera);
					papelera.setBackgroundResource(R.drawable.papelera);
					
					papelera.setVisibility(View.VISIBLE);
					papelera.startAnimation(animationLeft);
					
				}else if(papelera != null && View.INVISIBLE == papelera.getVisibility()){
					papelera.startAnimation(animationLeft);
					papelera.setVisibility(View.VISIBLE);
				}else{
					if(papelera != null && View.VISIBLE == papelera.getVisibility()){
						papelera.startAnimation(animationRight);
						papelera.setVisibility(View.INVISIBLE);
					}
				}
				
				holder.setBotonPapelera(papelera);
				
				return true;
			}
        });
	}
	
	public String getAddressFromGPSData(double lat, double longi) {
		HttpRetriever agent = new HttpRetriever();
		String request = GOOGLE_GEOCODER + lat + "," + longi + "&sensor=false&language=es";
		String response = agent.retrieve(request);
		String formattedAddress = "";
		String calle = "";
		String ciudad = "";
		if (response != null) {
			try {
				JSONObject parentObject = new JSONObject(response);
				JSONArray arrayOfAddressResults = parentObject.getJSONArray("results");
				JSONObject addressItem = arrayOfAddressResults.getJSONObject(0);
				
				JSONArray arrayOfAddress = addressItem.getJSONArray("address_components");
				
				for(int i = 0; i < arrayOfAddress.length(); i++){
					JSONObject address = arrayOfAddress.getJSONObject(i);
					String tipo = address.getString("types");
					if(tipo.equalsIgnoreCase("[\"route\"]")){
						 calle = address.getString("long_name");
					}
					if(tipo.equalsIgnoreCase("[\"locality\",\"political\"]")){
						 ciudad = address.getString("long_name");
					}
				}					
				formattedAddress = calle + ", " + ciudad + ".";
				
			} catch (JSONException e) {

				e.printStackTrace();
			}

		}
		return formattedAddress;
	}

	@Override
	public View newView(Context context, final Cursor cursor, ViewGroup parent) {
		 View v = mLayoutInflater.inflate(R.layout.listaeventositem, parent, false);		 
		 ViewHolder holder;
		 holder = new ViewHolder(v);
		 v.setTag(holder);
	     return v;
	}
	
	public class ViewHolder {
		
		private TextView textViewNombre = null;
		private TextView textViewComentario = null;
		private TextView textViewDireccion = null;
		private Button botonPapelera = null;
		private View base;
		
		public ViewHolder(View base){
			this.base = base;
		}

		public TextView getTextViewNombre() {
			return textViewNombre;
		}

		public void setTextViewNombre(TextView textViewNombre) {
			this.textViewNombre = textViewNombre;
		}

		public TextView getTextViewComentario() {
			return textViewComentario;
		}

		public void setTextViewComentario(TextView textViewComentario) {
			this.textViewComentario = textViewComentario;
		}

		public View getBase() {
			return base;
		}

		public void setBase(View base) {
			this.base = base;
		}

		public Button getBotonPapelera() {
			return botonPapelera;
		}

		public void setBotonPapelera(Button botonPapelera) {
			this.botonPapelera = botonPapelera;
		}

		public TextView getTextViewDireccion() {
			return textViewDireccion;
		}

		public void setTextViewDireccion(TextView textViewDireccion) {
			this.textViewDireccion = textViewDireccion;
		}
	}
}
